import { Injectable } from '@nestjs/common'
import { CreateTopicDto } from './dto/create-topic.dto'
import { UpdateTopicDto } from './dto/update-topic.dto'
import { Topic } from './entities/topic.entity'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository, Like } from 'typeorm'
@Injectable()
export class TopicService {
  constructor(
    @InjectRepository(Topic) private readonly topic: Repository<Topic>
  ) {}

  create(createTopicDto: CreateTopicDto) {
    const data = new Topic()
    data.name = createTopicDto.name
    createTopicDto.avatar && (data.avatar = createTopicDto.avatar)

    return this.topic.save(data)
  }
  findByNameLike(query) {
    return this.topic.find({ where: { name: Like(`%${query.keyword}%`) } })
  }
  findAll() {
    return this.topic.find()
  }

  findOne(id: string) {
    return `This action returns a #${id} topic`
  }

  update(id: number, updateTopicDto: UpdateTopicDto) {
    return `This action updates a #${id} topic`
  }

  remove(id: number) {
    return `This action removes a #${id} topic`
  }
}
